<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="销售部门id" prop="saleDeptId">
        <el-input v-model="dataForm.saleDeptId" placeholder="销售部门id"></el-input>
      </el-form-item>
      <el-form-item label="销售部门名称" prop="saleDeptName">
        <el-input v-model="dataForm.saleDeptName" placeholder="销售部门名称"></el-input>
      </el-form-item>
      <el-form-item label="加工部门id" prop="workDeptId">
        <el-input v-model="dataForm.workDeptId" placeholder="加工部门id"></el-input>
      </el-form-item>
      <el-form-item label="加工部门名称" prop="workDeptName">
        <el-input v-model="dataForm.workDeptName" placeholder="加工部门名称"></el-input>
      </el-form-item>
      <el-form-item label="状态(1-启用,0-禁用)" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态(1-启用,0-禁用)"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
      </el-form-item>
      <el-form-item label="创建用户id" prop="createUser">
        <el-input v-model="dataForm.createUser" placeholder="创建用户id"></el-input>
      </el-form-item>
      <el-form-item label="修改用户id" prop="modifyUser">
        <el-input v-model="dataForm.modifyUser" placeholder="修改用户id"></el-input>
      </el-form-item>
      <el-form-item label="租户id" prop="tenantId">
        <el-input v-model="dataForm.tenantId" placeholder="租户id"></el-input>
      </el-form-item>
      <el-form-item label="组织id" prop="orgId">
        <el-input v-model="dataForm.orgId" placeholder="组织id"></el-input>
      </el-form-item>
      <el-form-item label="单位id" prop="unitId">
        <el-input v-model="dataForm.unitId" placeholder="单位id"></el-input>
      </el-form-item>
      <el-form-item label="逻辑删除(1-正常,-1已删除)" prop="delFlag">
        <el-input v-model="dataForm.delFlag" placeholder="逻辑删除(1-正常,-1已删除)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getWorkDeptObj,
    addWorkDeptObj,
    putWorkDeptObj
  } from '@/api/eims/work-dept.js'

  export default {
    data() {
      return {
				ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          saleDeptId: '',
          saleDeptName: '',
          workDeptId: '',
          workDeptName: '',
          status: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
          delFlag: ''
        },
        dataRule: {
          saleDeptId: [{
            required: true,
            message: '销售部门id不能为空',
            trigger: 'blur'
          }],
          saleDeptName: [{
            required: true,
            message: '销售部门名称不能为空',
            trigger: 'blur'
          }],
          workDeptId: [{
            required: true,
            message: '加工部门id不能为空',
            trigger: 'blur'
          }],
          workDeptName: [{
            required: true,
            message: '加工部门名称不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态(1-启用,0-禁用)不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建用户id不能为空',
            trigger: 'blur'
          }],
          modifyUser: [{
            required: true,
            message: '修改用户id不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }],
          orgId: [{
            required: true,
            message: '组织id不能为空',
            trigger: 'blur'
          }],
          unitId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '逻辑删除(1-正常,-1已删除)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWorkDeptObj(this.dataForm.id).then(response => {
							if(response.data.code == '0'){
							  this.dataForm = response.data.data
							}else{
							  this.$message.error(response.data.msg)
							}
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putWorkDeptObj(this.dataForm).then(response => {
								if(response.data.code == '0'){
								  this.$message.success('修改成功')
								  this.visible = false
								  this.$emit('refreshDataList')
								}else{
								  this.$message.error(response.data.msg)
								}
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addWorkDeptObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
						this.ischeck = false
					}
        })
      }
    }
  }
</script>
